package com.aaa.member.util;

import com.aaa.member.entity.Client;

import com.aaa.member.service.ClientService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;


public class MyRealm extends AuthorizingRealm {
    @Autowired
    private ClientService clientService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取收集到的userName
        String userName = (String) authenticationToken.getPrincipal();
        //根据用户名查找用户信息
        Client client = (Client) clientService.queryClientByParam(userName).getData();
        //用户的user为null  说明当前用户名查询不到结果
        if (client == null) {
            throw new AccountException();

        }
        // 用户名正确时，把加密加盐后的密码和盐值都交给SecurityManager  进行认证
        //  用户信息  用户密码  二进制转换  当前运行类类名
        return new SimpleAuthenticationInfo(client, client.getPassword(), ByteSource.Util.bytes(client.getPwSalt()), this.getName());
    }

    public static void main(String[] args) {
        //System.out.println((UUID.randomUUID() + "").length());
        Client client = new Client();
        System.out.println(new SimpleAuthenticationInfo(client, "123456", ByteSource.Util.bytes(1024), MyRealm.class.getName()));
    }
}
